<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useMainLogin } from '@/store/user'
import { useMainStore } from '@/store/index'
import { resetPwd,smsResetPwd } from '@/api/user'
import { startDrive } from "@/utils/help";
import type { FormInstance } from 'element-plus'
import * as mars3d from "mars3d"

const Cesium = mars3d.Cesium
const user = useMainLogin()
const store = useMainStore()
const xgFlag = ref<boolean>(true)
const dialogTableVisible = ref<boolean>(false)
const mapChecked = ref<number>(0)
const form = ref({oldPassword:'',password: '',smsCode:''})
const rules = ref({
  oldPassword: [{ required: true, trigger: 'blur', message: "密码格式不正确,请输入6位及以上作为密码", pattern:/^[0-9A-Za-z]{6,}$/ }],
  password: [{ required: true, trigger: 'blur', message: "密码格式不正确,请输入6位及以上作为密码", pattern:/^[0-9A-Za-z]{6,}$/ }],
})

const platType = ref<string>('0')
const reSetPassWard = ref<FormInstance>()

const checked1 = ref(true)
const checked2 = ref(true)
const checked3 = ref(true)
const checked4 = ref(true)
const baseurl = import.meta.env.VITE_APP_BASE_WmsUrl
const name = import.meta.env.VITE_APP_BASE_WmsName

onMounted(async () => {
  showMap()
  // addWmslayer(baseurl,`road_defect_inspection${name}:公里桩WGS84${name}`,'glz')
  // addWmslayer(baseurl,`road_defect_inspection${name}:百米桩WGS84${name}`,'bmz')

  addGeoJson('http://139.9.173.189:8080/geoserver/road_defect_inspection_prod/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=road_defect_inspection_prod%3A%E5%85%AC%E9%87%8C%E6%A1%A9WGS84_prod&maxFeatures=50&outputFormat=application%2Fjson','glz')
  addGeoJson('http://139.9.173.189:8080/geoserver/road_defect_inspection_prod/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=road_defect_inspection_prod%3A%E7%99%BE%E7%B1%B3%E6%A1%A9WGS84_prod&maxFeatures=50&outputFormat=application%2Fjson','bmz')
})

const addWmslayer = (url,layers,id) => {
    let urlstr = resetLayerUrl(url)
    let layer = new mars3d.layer.WmsLayer({
    id: id,
    url: urlstr,
    layers: layers,
    enablePickFeatures:false,
    parameters: {
        transparent: true,
        format: "image/png",
        format_options: "partials:false;group:true",
    },
  });
  window.map.addLayer(layer)
}

const addGeoJson = (url,id) => {
    let urlstr = resetLayerUrl(url)
    let layer = new mars3d.layer.GeoJsonLayer({
    id: id,
    url: urlstr,
    symbol: {
      styleOptions: {
        // opacity: 0.1,
        label: {
          text: "{pile_num}",
          show: true,
          position: "center",
          font_family: "楷体",
          font_size: 24,
          color: id=='glz'?'#ff0000':'#ffffff',
          outline:true,
          outlineColor: '#73b273',
          outlineWidth:10,
          scaleByDistance:true,
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0,2500)
        }
      }
    }
  });
  window.map.addLayer(layer)
}


// 修改url
const resetLayerUrl = (url:any) => {
  var arr = []
  var urlstr = ''
  if(url.indexOf('zxchhwy')>0 || url.indexOf('zxchjfj')>0 || url.indexOf('zxchjfn')>0 || url.indexOf('zxchjf')>0){
    return url
  }
  if(url.indexOf('zxch.cpolar.cn') > 0){
    arr = url.split('zxch.cpolar.cn')
    urlstr = '/zxchhwy' + arr[1]
  }
  else if(url.indexOf('139.9.173.189:8090') > 0){
    arr = url.split('139.9.173.189:8090')
    urlstr = '/zxchjfj' + arr[1]
  }
  else if(url.indexOf('139.9.173.189:8091') > 0){
    arr = url.split('139.9.173.189:8091')
    urlstr = '/zxchjfn' + arr[1]
  }
  else if(url.indexOf('139.9.173.189:8080') > 0){
    arr = url.split('139.9.173.189:8080')
    urlstr = '/zxchjf' + arr[1]
  }else{
    ElMessage({
      type: 'warning',
      message: '该资源禁止上传!'
    });
    return false
  }
  return urlstr
}

const changeWms = (id) => {
   const layer = window.map.getLayerById(id);
   if(layer){
    if(id == 'glm'){
      checked1.value = layer.show = !layer.show
    }else if(id == 'glz'){
      checked2.value = layer.show = !layer.show
    }else if(id == 'bmz'){
      checked3.value = layer.show = !layer.show
    }else if(id == 'lczm'){
      checked4.value = layer.show =!layer.show
    }
   }
}
const showMap = () => {
  let base = window.map.getBasemaps()
  base.forEach((element,index) => {
    element.show=index==platType.value?true:false
  });
  mapChecked.value=platType.value*1
  base[base.length-1].show = true
}

const changelayer = (number,type?) => {
  let base = window.map.getBasemaps()
  base.forEach((element,index) => {
    element.show=index==platType.value?true:false
  });
  base[base.length-1].show = true
  base[number].show = true
}

const cancelAddUser = () => {
  dialogTableVisible.value = false
  form.value = {oldPassword:'',password: '',smsCode:''}
}

const reSetPasswordSelf = () => {
  reSetPassWard.value.validate().then(valid=> {
    if(!valid) return
    if(xgFlag.value) {
      resetPwd({oldPassword:form.value.oldPassword,password:form.value.password}).then(res => {
        if(res.code === 200) {
          ElMessage({type: 'success',message: '修改成功!'});
          dialogTableVisible.value = false
          ElMessageBox.alert('登录状态已过期，请重新登录','系统提示',{
            confirmButtonText: '重新登录',
            type: 'warning',
          }).then(() => {
            const useLogin = useMainLogin()
            useLogin.LogOut().then(() => {
              location.href = import.meta.env.VITE_APP_CONTEXT_PATH + "login";
            })
          })
        }
      })
    }else {
      smsResetPwd({smsCode:form.value.smsCode,password:form.value.password}).then(res => {
        if(res.code === 200) {
          ElMessage({type: 'success',message: '修改成功!'});
          dialogTableVisible.value = false
          ElMessageBox.alert('登录状态已过期，请重新登录','系统提示',{
            confirmButtonText: '重新登录',
            type: 'warning',
          }).then(() => {
            const useLogin = useMainLogin()
            useLogin.LogOut().then(() => {
              location.href = import.meta.env.VITE_APP_CONTEXT_PATH + "login";
            })
          })
        }
      })
    }
  })
}

const operatingInstructions = () => {
  user.isFirst = true
  startDrive()
}
</script>

<template>
  <div class="TabbarBtn">
    <el-tooltip
      class="box-item"
      effect="dark"
      content="用户"
      placement="left"
    >
    <div class="iconBox" @click="store.activeBtn==1?store.activeBtn=0:store.activeBtn=1">
      <img src="@/assets/image/user.png" alt="">
    </div>
    </el-tooltip>
    <div :class="store.activeBtn==1?'btnTooltip show':'btnTooltip'" >
      <span class="listBox" style="padding: 40px 10px 10px;">
        <i @click="store.activeBtn=0" class="iconfont icon-cuo" style="position: absolute;right: 10px; top: 10px;cursor: pointer;"></i>
        <span class="tooltip">
            <!-- <div class="tooltip_box" @click="operatingInstructions()">新手学习</div> -->
            <div class="tooltip_box" @click="dialogTableVisible = true">修改密码</div>
            <div class="tooltip_box" @click="user.LogOut()">退出登陆</div>
          </span>
      </span>
    </div>
  </div>
  <!-- <div style="margin-bottom: 70px;"></div> -->
  <div class="TabbarBtn">
    <el-tooltip
      class="box-item"
      effect="dark"
      content="图层"
      placement="left"
    >
    <div :class="store.activeBtn==2?'iconBox show':'iconBox'" @click="store.activeBtn==2?store.activeBtn=0:store.activeBtn=2">
      <img src="@/assets/image/layer.png" alt="">
    </div>
    </el-tooltip>
    <div :class="store.activeBtn==2?'btnTooltip show':'btnTooltip'">
      <span class="listBox" style="padding-top: 20px;">
        <i @click="store.activeBtn=0" class="iconfont icon-cuo" style="position: absolute;right: 10px; top: 10px;cursor: pointer;"></i>
        <el-radio-group class="listBox-layer" v-model="mapChecked">
          <el-radio :label="0" @click="changelayer(0)">卫星影像</el-radio>
          <el-radio :label="1" @click="changelayer(1)">电子地图</el-radio>
          <!-- <el-checkbox v-model="checked1" label="道路矢量" @change="changeWms('glm')" /> -->
          <el-checkbox v-model="checked2" label="千里桩" @change="changeWms('glz')" />
          <el-checkbox v-model="checked3" label="百米桩" @change="changeWms('bmz')" />
          <!-- <el-checkbox v-model="checked4" label="里程桩面" @change="changeWms('lczm')" /> -->
          <!-- <el-radio :label="5" @click="changelayer(2)">高德影像</el-radio> -->
        </el-radio-group>
      </span>
    </div>
  </div>

  <!-- 修改密码 表单 -->
  <el-dialog v-model="dialogTableVisible" >
    <el-form :model="form" :rules="rules" ref="reSetPassWard">
      <div class="xgmmTab">
        <span :class="xgFlag?'select':''">旧密码修改密码</span>
      </div>
        <!-- <el-form-item label="旧密码" v-show="xgFlag">
          <el-input v-model="form.oldPassword" autocomplete="off" type="password" placeholder="首次设置密码无需填写" />
        </el-form-item> -->
        <el-form-item label="旧密码" prop="password">
          <el-input v-model="form.oldPassword" autocomplete="off" type="password" placeholder="首次设置密码无需填写" />
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" autocomplete="off" type="password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelAddUser">取消</el-button>
          <el-button type="primary" @click="reSetPasswordSelf()">提交</el-button>
        </span>
      </template>
  </el-dialog>
</template>

<style>
.el-dialog{
  background-color: var(--app-background) !important;
}
.el-input__inner {
  background-color: transparent!important;
}
.xgmmTab {
  display: flex;
  margin-bottom: 20px;
}
.xgmmTab span {
  padding: 0 5px 4px 5px;
  cursor: pointer;
  margin-right: 10px;
}
.xgmmTab .select {
  color: rgb(255, 106, 0);
  border-bottom: .125rem solid rgb(255, 106, 0);
}
.tooltip {
  overflow: hidden;
  display: block;
}

.tooltip_box{
  width: 100%;
  cursor: pointer;
  user-select: none;
  padding: 10px 0 0 0;
  display: flex;
  align-items: center;
}
.tooltip_box:hover,
.tooltip_box .path {
  color: #3DBDA7;
}
.tooltip_box .path {
    stroke: #3DBDA7;
}
/* 单选按钮 */
.listBox-layer{
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.listBox-layer .el-radio{
  width: 100%;
  margin-right: 0;
}
.listBox-layer .el-radio__inner{
  background-color: transparent;
  border-color: #3DBDA7;
  border-width: 2px;
  /* border-radius: 3px; */
  height: 16px;
  width: 16px;
}
.listBox-layer .el-radio__label{
  color: #fff;
  font-size: 15px;
}
.listBox-layer .el-radio__input.is-checked .el-radio__inner{
  border-color: #3DBDA7;
  background: #3DBDA7;
}
.listBox-layer .el-radio__inner::after{
  background-color: rgba(29, 30, 34, .8);
}
.listBox-layer .el-radio:hover .el-radio__label,
.listBox-layer .el-radio__input.is-checked+.el-radio__label{
  color: #3DBDA7;
}
</style>
